package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private OrderDetailService orderDetailService;
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }
    //订单的分页查询
//    @GetMapping("/userPage")
//    public R<Page> page(Integer page,Integer pageSize){
//         //设置分页对象
//        Page<Orders> pageInfo= new Page<>(page,pageSize);
//        new Page<>()
//        long total = pageInfo.getTotal();
//        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(Orders::getUserId, BaseContext.getCurrentId());
//        Orders orders = new Orders
//
//
//
//
//    }
    @GetMapping("/userPage")
    public R<Page> page(int page, int pageSize) {
        Page<Orders>pageInfo= new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders>lqw = new LambdaQueryWrapper<>();
        lqw.eq(Orders::getUserId,BaseContext.getCurrentId());
        lqw.orderByDesc(Orders::getOrderTime);
        orderService.page(pageInfo,lqw);
        return  R.success(pageInfo);
    }
    @GetMapping("/page")
    public R<Page>page(Integer page,Integer pageSize, Orders orders){
        Page<Orders>pageInfo= new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        String number = orders.getNumber();
        lqw.like(StringUtils.isNotEmpty(number),Orders::getNumber,number);
        orderService.page(pageInfo);
//        Page<OrdersDto>  ordersDtoPage = new Page<OrdersDto>(page, pageSize);
//        LambdaQueryWrapper<OrdersDto> lqw= new LambdaQueryWrapper<>();
//        BeanUtils.copyProperties(pageInfo,ordersDtoPage,"records");


        return R.success(pageInfo);
    }
}
